const https = require('https')
const cheerio = require('cheerio')
const fs = require('fs')
const { resolve } = require('path')
const { rejects } = require('assert')

//获取页面的html结构
let html = ''
let movies = []



for (let index = 0; index < 10; index++) {
    const element = index * 25;
    https.get(`https://movie.douban.com/top250?start=${element}&filter=`, function (res) {
    
        res.on('data', function (chunk) {
            //得到数据流，通过字符串拼接得到html结构
            html += chunk
        })
    
        res.on('end', function () {
            // 获取html中的数据
            const $ = cheerio.load(html)
            
            $('li .item').each(function () {
                const title = $('.title', this).text()
                const star = $('.info .bd .rating_num', this).text()
                const pic = $('.pic img', this).attr('src')
                //数据以对象的形式存放在数组中
                movies.push({
                    title: title,
                    star: star,
                    pic: pic
                })
            }) 
        })
    })
}



setTimeout(() => {
    console.log(movies.length, 50)
    //将数据写入文件中
fs.writeFile('./douban.json', JSON.stringify(movies), function (err, data) {
    if (err) {
        throw err
    }
    console.log('文件保存成功');
})
}, 60000)
